package com.wangzhenyu.learn.test.kotlin.sealedClass

@Suppress("UNUSED")
object InstanceOfSealedClass {

    //密封类
    sealed class GameAction(times: Int) {

        val times: Int

        init {
            this.times = times
        }

        object Start : GameAction(1)
        data class AutoTick(val time: Int) : GameAction(2)
        class Exit : GameAction(3)
    }

    object Restart : GameAction(4)


    sealed interface Action

    enum class GamesAction : Action {
        Start, Exit, Restart
    }

    enum class BirdsAction : Action {
        Up, Down, HitGround, HitPipe, CrossedPipe
    }

    enum class PipesAction : Action {
        Move, Reset
    }

    enum class RoadsAction : Action {
        Move, Reset
    }

    object Tick : Action

    //使用的时候就可以对抽成的 Action 进行嵌套判断
    fun dispatch(action: Action) {
        when (action) {

            Tick -> TODO()

            is GamesAction -> {
                when (action) {
                    GamesAction.Start -> TODO()
                    GamesAction.Exit -> TODO()
                    GamesAction.Restart -> TODO()
                }
            }
            is BirdsAction -> {
                when (action) {
                    BirdsAction.Up -> TODO()
                    BirdsAction.Down -> TODO()
                    else -> TODO()
                }
            }
            is PipesAction -> {
                when (action) {
                    PipesAction.Move -> TODO()
                    PipesAction.Reset -> TODO()
                }
            }
            is RoadsAction -> {
                when (action) {
                    RoadsAction.Move -> TODO()
                    RoadsAction.Reset -> TODO()
                }
            }
        }
    }
}
//总体来说 sealed class 和 interface 和 enum（枚举） 有相近的地方，也有明显区别，需要留意：
//每个 enum 常量只能以单例的形式存在
//sealed class 子类可以拥有多个实例，不受限制，每个均可以拥有自己的状态
//enum class 不能扩展自 sealed class 以及其他任何 Class，但他们可以实现 sealed interface